﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Autofac;
using Moq;
using Ue.CQRS;
using Ue.CQRS.Bus;
using Ue.CQRS.Events;
using Ue.CQRS.DistributionManager;

namespace Ue.CQRS.Tests.Events
{
    /// <summary>
    /// EventSource 的摘要说明
    /// </summary>
    [TestClass]
    public class EventSourceTests
    {
        public IList<IEvent> sentEvents;
        public IList<IEvent> distributoredEvents;

        [TestInitialize]
        public void Initial()
        {
            this.sentEvents = new List<IEvent>();
            this.distributoredEvents = new List<IEvent>();

            var mockBus = new Mock<IBus>();
            mockBus.Setup(bus => bus.Send(It.IsAny<IEvent>()))
                .Callback<IEvent>(domainEvent => sentEvents.Add(domainEvent));

            var mockDistributor = new Mock<IDistributor>();
            mockDistributor.Setup(distributor => distributor.Dispatch(It.IsAny<IEvent>()))
                .Callback<IEvent>(domainEvent => distributoredEvents.Add(domainEvent));

            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterInstance<IBus>(mockBus.Object).SingleInstance();
            containerBuilder.RegisterInstance<IDistributor>(mockDistributor.Object).SingleInstance();

            var appContext = new ApplicationContext(containerBuilder);

            appContext.Start();
        }

        #region EventSource Name and Id

        [TestMethod]
        public void EventSourceNameDefualtIsTypeShortName()
        {
            var eventSource = EventSourceFactory.Build(new FakeSourceModel());

            Assert.AreEqual(typeof(FakeSourceModel).Name,eventSource.Name);
            Assert.AreEqual("FakeSourceModel", eventSource.Name);
        }

        [TestMethod]
        public void DesignateEventSourceName()
        {
            var eventSource = EventSourceFactory.Build(new FakeSourceModel2());

            Assert.AreEqual("FakeSourceModelName", eventSource.Name);
        }
        
        //[TestMethod]
        //public void EventSourceIdentityDefualtIsNewGuid()
        //{
        //    var eventSource = EventSourceFactory.Instance.GetSource(new FakeSourceModel());

        //    Assert.IsNotNull(eventSource.Id);
        //    Assert.AreNotEqual(Guid.Parse("00000000-0000-0000-0000-000000000000"), eventSource.Id);
        //}

        #endregion

        #region EventSource


        #endregion
    }

    [EventSource]
    public class FakeSourceModel
    {
    }

    [EventSource("FakeSourceModelName")]
    public class FakeSourceModel2
    {

    }
}
